package com.itheima.reggie.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 加入IOC容器
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    //获取dao层对象
    @Autowired(required = false)
    private CategoryMapper categoryMapper;
    @Autowired(required = false)
    private DishMapper dishMapper;
    @Autowired(required = false)
    private SetmealMapper setmealMapper;

    /**
     * 添加套餐分类菜系
     *
     * @param category
     * @return
     */
    @Override
    public boolean save(Category category) {
        //调用分类信息查询菜系名是否重复
        Category name = categoryMapper.findByName(category.getName());
        if (name != null) {
            //菜系名重复,抛异常
            throw new BusinessException("菜系名已存在,请重新输入");
        }
        //添加菜系信息
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        //调用dao添加方法保存菜系信息 >0 表示添加成功
        return categoryMapper.save(category) > 0;
    }

    /**
     * 分页查询
     *
     * @return
     */
    @Override
    public Page<Category> findAll(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Category> categoryList = categoryMapper.findAll();
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        Page<Category> pageObj = new Page<>();
        pageObj.setRecords(pageInfo.getList());
        pageObj.setTotal(pageInfo.getTotal());
        return pageObj;
    }

    /**
     * 修改菜系
     *
     * @param category
     * @return boolean
     */
    @Override
    public boolean update(Category category) {
        //传入修改时间
        category.setUpdateTime(LocalDateTime.now());
        //修改菜系是否重名
        Category byName = categoryMapper.findByName(category.getName());
        if (byName != null && !byName.getId().equals(category.getId())) {
            throw new BusinessException("修改菜系名已存在,请重新输入");      //有个问题  && byName.getId().equals(category.getId())
        }
        return categoryMapper.update(category) > 0;
    }

    /**
     * 删除菜系或套餐
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        //判断菜系是否有数据
        if (dishMapper.findById(id) > 0) {
            throw new BusinessException("菜系有数据,请删除数据再删除");
        }
        //判断套餐是否有数据
        if (setmealMapper.findById(id) > 0) {
            throw new BusinessException("套餐有数据,请删除数据再删除");
        }
        //都没有问题执行删除业务
        return categoryMapper.delete(id) > 0;
    }

    /**
     * 根据类型查询分类信息列表
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> findAllByPage(Integer type) {
        return categoryMapper.findAllByPage(type);
    }
}
